<?php
/**
 * 矩阵管理
 */
namespace app\api\controller;
use app\api\model\BaseConfig as BaseConfigModel;
use app\api\model\Member as MemberModel;
use app\common\common;
use app\common\model\MemberMatrix as MemberMatrixModel;
use think\facade\Db;
use app\common\model\UserMoneyLog as UserMoneyLogModel;

use think\facade\Cache;

class Matrix extends Baek
{
    /**
     * 参与矩阵参数
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function data_config(){
        $data = [
            'minimum_quantity'  =>  BaseConfigModel::config_data('participate_num'),
            'multiple'  =>  BaseConfigModel::config_data('participate_multiple'),
        ];
        return api_success($data);
    }

    /**
     * 参与矩阵
     * @return \think\Response
     */
    function pay(){
        $money  = $this->request->post('money', '', 'intval');  //购买金额
        $type  = $this->request->post('type', '', 'intval');  //支付类型 1:余额 2:链钱包

        //最低参与金额
        $participate_num = BaseConfigModel::config_data('participate_num');
        if($participate_num > $money) return api_error('','Not less than the minimum purchase amount',$this->language);

        //参与矩阵金额倍数
        $participate_multiple = BaseConfigModel::config_data('participate_multiple');
        if(empty($participate_multiple) || $participate_multiple == 0){
            if($money%$participate_multiple != 0) return api_error('','Wrong multiple of participation quantity',$this->language);
        }

        BaseConfigModel::config_data('usdt_key');
        Db::startTrans();
        try {
            $list = MemberMatrixModel::getUserList($this->mid,1,20);

            //创建订单
            $id = MemberMatrixModel::add($this->mid, $money, $type);
            if(!$id){
                Db::rollback();
                return api_error('','Order creation failed',$this->language);
            }

            if($type == 1){ //余额支付
                $pay_status = UserMoneyLogModel::change($this->mid,'usdt',-$money,'pay_matrix',$id,'购买矩阵，支付：'.$money);
                if($pay_status != true){  //支付失败
                    Db::rollback();
                    return api_error($pay_status,'Purchase failed',$this->language);
                }
                
                
                
                //赠送积分
                $mid = $this->mid;
                $where = [
                    'mid'    =>  $mid,
                    'id'    =>  $id,
                ];
                $model = new \app\common\model\MemberMatrix;
                try {
                    $data = [];
                    $info = $model::where($where)->find();
                    $data['username'] = \app\api\model\Member::where('id', $mid)->value('username');
                    $data['point'] = $info['money'];
                    $data['time'] = time();
                    $data['key'] = '666789';
                    $data2 = $data;
                    sort($data2);
                    $data['sign'] = md5(http_build_query($data2));
                    unset($data['key']);
                    $data = base64_encode(json_encode($data));

                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, "https://wap.mmstio.com/api/register/point?data=".$data);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                    curl_setopt($ch, CURLOPT_HEADER, 0);
                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
                    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
                    $output = curl_exec($ch);
                    curl_close($ch);
                    $res = json_decode($output,true);
                    if($res['code'] == 0){
                        $model::where($where)->update(['point'=>2]);
                    }
                }catch(\Exception $e){

                }
                
                
                
                
                //发放矩阵奖励
                common::juzhenjiangli($id,$money,$this->mid);
                
                Cache::inc('matrix',1);
                Cache::inc('matrix'.date('Y'),1);
                
            }

            Db::commit();
            return api_success($id);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return api_error('','Purchase failed',$this->language);
        }
    }

    /**
     * 我参与的记录
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfo(){
        $id  = $this->request->post('id', '', 'intval');  //购买金额
        $data  = [];
        //矩阵图
        $data['juzhen'] = $this->common->member_matrix($this->mid);
        //查询余额
        $data['coin']   = MemberModel::wallet($this->mid);
        //查询详情
        $data['info'] = MemberMatrixModel::getMemberDetails($this->mid,$id);

        return api_success($data);
    }

    /**
     * 获取用户参与矩阵记录
     * @return \think\Response
     * @throws \think\db\exception\DbException
     */
    function getList(){
        $page = $this->request->post('page', 1, 'intval');
        $limit = $this->request->post('limit', 20, 'intval');
        $data = MemberMatrixModel::getUserList($this->mid,$page,$limit);
        $data['reward'] = MemberMatrixModel::statistics($this->mid);
        return api_success($data);
    }

    /**
     * 查询某个用户的13矩阵图
     * @return \think\Response
     */
    function getMmatrix(){
        $id  = $this->request->post('id', '', 'intval');  //用户ID
        $data = $this->common->member_matrix($id);
        return api_success($data);
    }



}